package com.yincheng.oms.sso.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yincheng.oms.sso.entity.MenuAuth;
import com.yincheng.oms.sso.entity.RoleMenu;
import com.yincheng.oms.sso.entity.UserRole;
import com.yincheng.oms.sso.mapper.MenuAuthMapper;
import com.yincheng.oms.sso.mapper.RoleMenuMapper;
import com.yincheng.oms.sso.mapper.UserRoleMapper;

@Service
public class MenuAuthService {

	@Autowired
	private UserRoleMapper userRoleMapper;
	
	@Autowired
	private MenuAuthMapper menuAuthMapper;
	
	@Autowired
	private RoleMenuMapper roleMenuMapper;
	
	/**
	 * 根据用户id获取用户所有的菜单权限列表
	 * @param userId
	 * @return
	 */
	public List<MenuAuth> getMenuAuthList(String userId){
		List<MenuAuth> menuAuthList = new ArrayList<MenuAuth>();
		List<UserRole> userRoleList = (List<UserRole>) CollectionUtils.emptyIfNull(userRoleMapper.selectByUserId(userId));
		for (UserRole userRole : userRoleList) {
			String roleId = userRole.getRoleId();
			List<RoleMenu> roleMenuAuthList = (List<RoleMenu>) CollectionUtils.emptyIfNull(roleMenuMapper.selectByRoleId(roleId));
			for (RoleMenu roleMenuAuth : roleMenuAuthList) {
				Integer menuId = roleMenuAuth.getMenuId();
				MenuAuth menuAuth = menuAuthMapper.selectByPrimaryKey(menuId);
				menuAuthList.add(menuAuth);
			}
		}
		menuAuthList.sort((c1,c2) -> c1.getOrderNum().compareTo(c2.getOrderNum()));
		return menuAuthList;
	}
}
